<?php

namespace App\Http\Controllers;

use App\Common\Constant\Params;
use App\Http\Requests\ArticleAddPost;
use App\Libs\Filter\Filter;
use App\Models\Article;
use App\Models\Banner;
use App\Services\IArticleService;
use App\Services\IStorageService;
use App\Services\ISystemService;
use Illuminate\Http\Request;

/**
 * 文章管理
 * Class ArticleController
 * @package App\Http\Controllers
 */
class ArticleController extends BaseController
{

    /**
     * 所有文章
     * @param Request $request
     */
    public function all(Request $request,IArticleService $articleService)
    {
        $filter = Filter::instance()
            ->HTML('文章标题：')
            ->input('title')
            ->HTML('文章内容：')
            ->input('content')
            ->HTML('添加时间：')
            ->date('add_time')
            ->br()->br()
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $articleService->all($filter->get_where());
        }else{
            $list = $articleService->all();
        }
        return view('article/all',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 添加文章
     * @param Request $request
     */
    public function add(ArticleAddPost $request,IArticleService $articleService)
    {
        if ($request->isMethod('post')){
            $articleService->add($request->input('title'),$request->input('content'),$request->input('cat_id'));
            return redirect('article/all');
        }
        return view('article/add',['categries'=>Params::$ARTICLE_CATEGORY]);
    }

    /**
     * 前台显示文章
     * @param Request $request
     */
    public function show(Article $article,Request $request,IArticleService $articleService)
    {
        return view('article/show',['article'=>$article]);
    }

    /**
     * 编辑文章
     * @param Request $request
     */
    public function edit(Article $article,ArticleAddPost $request,IArticleService $articleService)
    {
        if ($request->isMethod('post')){
            $articleService->edit($article->id,$request->input('title'),$request->input('content'),$request->input('cat_id'));
            return redirect('article/all');
        }
        return view('article/edit',['article'=>$article,'categries'=>Params::$ARTICLE_CATEGORY]);
    }

    /**
     * 删除文章
     * @param Request $request
     */
    public function delete(Article $article,Request $request,IArticleService $articleService)
    {
        $articleService->delete($article->id);
        return back();
    }

    /**
     * 所有banner
     * @param Request $request
     */
    public function bannerList(Request $request,IArticleService $articleService)
    {
        $list = $articleService->bannerList();
        return view('article/bannerList',['list'=>$list]);
    }

    /**
     * 添加banner
     * @param Request $request
     */
    public function bannerAdd(Request $request,IArticleService $articleService,ISystemService $systemService,IStorageService $storageService)
    {
        if ($request->isMethod('post')){
            $banner_img = $storageService->saveUpload('_banner_img');
            $ret = $articleService->bannerAdd($request->input('name'),$banner_img,$request->input('sort'),$request->input('url'));
            if ($ret){
                $systemService->success('添加成功');
            }
            return redirect('article/bannerlist');
        }
        return view('article/bannerAdd');
    }

    /**
     * 编辑banner
     * @param Request $request
     */
    public function bannerEdit(Banner $banner,Request $request,IArticleService $articleService,ISystemService $systemService,IStorageService $storageService)
    {
        if ($request->isMethod('post')){
            if (!empty($request->file('_banner_img'))){
                $banner_img = $storageService->saveUpload('_banner_img');
            }else{
                $banner_img = $request->input('banner_img');
            }
            $ret = $articleService->bannerEdit($banner->id,$request->input('name'),$banner_img,$request->input('sort'),$request->input('url'));
            return redirect('article/bannerlist');
        }
        return view('article/bannerEdit',['banner'=>$banner]);
    }

    /**
     * 编辑banner
     * @param Request $request
     */
    public function bannerDelete(Banner $banner,IArticleService $articleService,ISystemService $systemService)
    {
        $ret = $articleService->bannerDelete($banner->id);
        if ($ret){
            $systemService->success('删除成功');
        }
        return back();
    }
}
